package com.xb.cloud.front.client;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xb.cloud.common.util.StringUtils;
import com.xb.cloud.front.common.FrontSearchService;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.product.search.service.client.bo.SearchRequestBo;

/**
 * 
 * @ClassName:  FrontSearchAdapter   
 * @Description:TODO
 * @author: 鲜备科技
 * @date:   2018年3月1日 下午1:53:50   
 *     
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved. 
 *
 */
@Component
public class FrontSearchAdapter extends Adapter {
	@Autowired
	private FrontSearchService frontSearchService;

	/**
	 * 
	 * <p>
	 * Title: execute
	 * </p>
	 * <p>
	 * Description:获取搜索数据
	 * </p>
	 * 
	 * @param urlPath
	 * @param isLogined
	 * @param bizInfo
	 * @param context
	 * @return
	 * @see com.xb.cloud.gateway.client.Adapter#execute(java.lang.String,
	 *      java.lang.Boolean, java.util.Map,
	 *      com.xb.cloud.gateway.client.AdapterContext)
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		RtnInfo rtnInfo = null;
		/**
		 * 获取搜索数据
		 */
		if (urlPath.toLowerCase().equals("/h5/search/getsmartkeyword")) {
			String keyword = bizInfo.get("keyword");
			SearchRequestBo requestBo = new SearchRequestBo();
			requestBo.setKeyword(keyword);
			if(!StringUtils.isEmpty(bizInfo.get("sortBy"))) {
				requestBo.setSortBy(Integer.valueOf(bizInfo.get("sortBy")));
			}
			if(!StringUtils.isEmpty(bizInfo.get("sortType"))) {
				requestBo.setSortType(Integer.valueOf(bizInfo.get("sortType")));
			}
			if(!StringUtils.isEmpty(bizInfo.get("pageNum"))) {
				
				requestBo.setPageNum(Integer.valueOf(bizInfo.get("pageNum")));
			}
			if(!StringUtils.isEmpty(bizInfo.get("pageSize"))) {
				
				requestBo.setPageSize(Integer.valueOf(bizInfo.get("pageSize")));
			}
			if(StringUtils.isEmpty(keyword)) {
				return  ReturnUtils.error(ReturnCodes.SEARCH_KEY_ILLEGAL);
			}
			rtnInfo = frontSearchService.searchByKeyword(requestBo);
			// 获取推荐数据
		}
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {

		return "/h5/search/**";
	}

}
